﻿@{
    ViewData["Title"] = "给用户设置角色";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
@section js{
    <script type="text/html" id="currentTableBar">
        <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="取消|授权" lay-filter="torole" {{ d.status ? 'checked' : '' }}>
    </script>
    <script>
    layui.use(['form', 'layer', 'table',  'element', 'common'], function () {
        var $ = layui.jquery,
            form = layui.form,
            apiUtil = layui.common,
            layer = layui.layer;

         apiUtil.render({
            elem: '#currentTableId',
             url: 'role/getListPagesByUser?userId=' +@ViewBag.UserId,
             toolbar: '#toolbarDemo',
            cols: [[
                { type: "checkbox", width: 50 },
                { field: 'id', width: 50, title: 'ID', sort: true },
                { field: 'name', width: 140, title: '角色名' },
                { field: 'description', title: '备注' },
                { title: '操作', minWidth: 100, toolbar: '#currentTableBar', align: "center" }
            ]],
            parseData: function (res) {
                return apiUtil.parseDataFun(res);
            }
        });
                        //监听授权操作
        form.on('switch(torole)', function (obj) {
            console.log("roleId:" + this.value)

                    var data = {
                        userId: @ViewBag.UserId,
                        roleId: this.value,
                        status: obj.elem.checked //? true : false
                    };
                    apiUtil.ajax('user/set-role', data, "application/json", "post", function (res) {
                        if (res.statusCode == 200 && res.success == true) {
                           // apiUtil.success(res.msg);
                            if (obj.elem.checked) {
                                apiUtil.success("授权成功~");
                            }
                            else {
                                apiUtil.error('取消授权~');
                            }
                        } else {
                            layer.msg(res.msg);
                            //if (obj.elem.checked) {
                            //    obj.elem.checked = false;
                            //} else {
                            //    obj.elem.checked = true;
                            //}
                        }
                    });
                });
    });
    </script>
}